//
//  GYInvoiceContentSelectedView.m
//  GoodLuck
//
//  Created by apple on 2018/4/3.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

static NSString *invoiceContentFilePath = @"invoiceContentFilePath";

#import "GYInvoiceContentSelectedView.h"
#import "GYNetWorkTools.h"
#import "NSString+FilePath.h"

@interface GYInvoiceContentSelectedView ()<UIPickerViewDelegate,UIPickerViewDataSource>

/** 数据模型 */
@property (nonatomic, strong) NSArray *invoiceContentList;

/** 当前的发票内容 */
@property (nonatomic, strong) NSString *currentInvoiceContent;

/** <#decrption#> */
@property (nonatomic, weak) UIPickerView *pickerView;
@end

@implementation GYInvoiceContentSelectedView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        //初始化
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI {
    self.backgroundColor = RGBColor(209, 212, 219);
    self.currentInvoiceContent = @"明细";
    
    //创建子控件
    UIView *competeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    competeView.backgroundColor = RGBColor(245, 245, 245);
    
    [self addSubview:competeView];
    
    UIButton *completeBtu = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-50, 0, 40, 40)];
    [completeBtu setTitle:@"完成" forState:UIControlStateNormal];
    [completeBtu addTarget:self action:@selector(completeBtuClick) forControlEvents:UIControlEventTouchUpInside];
    [completeBtu setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [competeView addSubview:completeBtu];
    
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 200)];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    //pickerView.backgroundColor = RGBColor(209, 212, 219);
    
    [self addSubview:pickerView];
    self.pickerView = pickerView;
    
    //加载数据;
    [self loadInvoiceContentList];
}

- (void)loadInvoiceContentList {
    NSArray *invoiceList = [[NSArray alloc] initWithContentsOfFile:[NSString CachesDir:invoiceContentFilePath]];
    if (invoiceList == nil) {
        [EasyLodingView showLodingText:@"正在加载..."];
        [GYNetWorkTools requestInvoiceContentList:^(NSDictionary *obj) {
            
            int code = [[obj objectForKey:@"code"] intValue];
            NSDictionary *datas = [obj objectForKey:@"datas"];
            if (code == 200) {
                [EasyLodingView hidenLoding];
                self.invoiceContentList = [datas objectForKey:@"invoice_content_list"];
                self.currentInvoiceContent = [self.invoiceContentList firstObject];
                [self.pickerView reloadAllComponents];
                
                //把数据缓存到沙盒
                BOOL isSuccess = [self.invoiceContentList writeToFile:[NSString CachesDir:invoiceContentFilePath] atomically:YES];
                if (isSuccess) {
                    NSLog(@"数据缓存成功....");
                }
            }else if (code == 400) {
                [EasyTextView showText:@"加载数据出错,请重新加载!"];
                [EasyLodingView hidenLoding];
            }
        } error:^(NSError *error) {
            NSLog(@"InvoiceContentListFailed=====%@",error);
            [EasyTextView showText:@"加载数据出错,请稍后加载!"];
            [EasyLodingView hidenLoding];
        }];
    }else {
        self.invoiceContentList = invoiceList;
        self.currentInvoiceContent = [self.invoiceContentList firstObject];
        [self.pickerView reloadAllComponents];
    }
}

- (void)completeBtuClick {
    self.selectInvoiceContentCallBack(self.currentInvoiceContent);
}


#pragma mark -- UIPickerViewDelegate,UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.invoiceContentList.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    return self.invoiceContentList[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.currentInvoiceContent = self.invoiceContentList[row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 44;
}





#pragma mark -- 懒加载

//- (NSArray *)invoiceContentList {
//    if (!_invoiceContentList) {
//        _invoiceContentList = @[@"明细",@"酒",@"食品",@"饮料",@"玩具",@"日用品",@"装修材料",@"化妆品",@"办公用品",@"学生用品",@"家居用品",@"饰品",@"服装"];
//    }
//    
//    return _invoiceContentList;
//}
@end
